<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Home\CommonController;
use App\Model\noticeModel;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;


class NoticeController extends CommonController
{
    //

    public function index()
    {

        $list = NoticeModel::paginate(24);

        return view('admin.notice.index',compact('list'));
    }


    public function create(){

        return view('admin.notice.add_notice');
    }


    public function store()
    {
        $input = Input::except('_token');
        $input['create_time'] = date('Y-m-d H:i:s');

        $rules = array(
            'notice_title'=>'required'
        );
        $msg = array(
            'notice_title.require'=>'新闻标题不能为空!'
        );
        $valid = Validator::make($input,$rules,$msg);


        if($valid->passes()){
            $add = NoticeModel::create($input);
            return redirect('Admin/notice');
        }else{
            return back()->withErrors($valid);
        }
    }


    // 编辑新闻
    public function edit($id)
    {
        $data = NoticeModel::find($id);
        return view('admin.notice.notice_edit')->with('data',$data);
    }

    public function update($id)
    {
        $input = Input::except('_method','_token');

        $rules = array(
            'notice_title'=>'required'
        );
        $msg = array(
            'notice_name.require'=>'新闻标题不能为空!'
        );
        $valid = Validator::make($input,$rules,$msg);


        if($valid->passes()){
            $add = NoticeModel::where('notice_id',$id)->update($input);
            return redirect('Admin/notice');
        }else{
            return back()->withErrors($valid);
        }
    }

    public function destroy($id)
    {
        $del = NoticeModel::where('notice_id',$id)->delete();
        $data = array(
            'code'=>0,
            'msg' =>'删除成功'
        );
        if(!$del){
            $data['code'] = 1001;
            $data['msg'] = "删除失败";
        }
        return $data;

    }
}

